package com.webcamtracker.ui.actions;

import com.webcamtracker.ui.target.UITargetModel;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;


public class SelectTargetAction implements Action {
    int state = 0;
    private PropertyChangeListener listener;
    public static final String SELECT_TARGET = "Select Traget";
    public static final String PICK_TARGET = "Pick Target";
    private static final String CLEAR_TARGET = "Clear Object";

    private UITargetModel manager;

    public SelectTargetAction(UITargetModel manager) {

        this.manager = manager;
    }

    public boolean isEnabled() {
        return true;
    }

    public void setEnabled(boolean b) {
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        this.listener = listener;
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        this.listener = null;
    }

    public Object getValue(String key) {
        String result = null;

        if (key.equals(Action.NAME)) {
            switch (state) {
                case 0:
                    result = SELECT_TARGET;
                    break;
                case 1:
                    result = PICK_TARGET;
                    break;
                case 2:
                    result = CLEAR_TARGET;
                    break;
            }
        }

        return result;
    }

    public void putValue(String key, Object value) {
    }

    public void actionPerformed(ActionEvent e) {
        switch (state) {
            case 0:
                manager.selectTarget();
                state++;
                listener.propertyChange(new PropertyChangeEvent(e.getSource(), Action.NAME, SELECT_TARGET,
                        PICK_TARGET));

                break;
            case 1:
                manager.pickTargetModel();
                state++;
                listener.propertyChange(new PropertyChangeEvent(e.getSource(), Action.NAME, PICK_TARGET,
                        CLEAR_TARGET));

                break;
            case 2:
                manager.clearTarget();
                state = 0;
                listener.propertyChange(new PropertyChangeEvent(e.getSource(), Action.NAME, CLEAR_TARGET,
                        SELECT_TARGET));

                break;
        }
    }
}
